﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using WeiXinPF.Modules.CDRT.SmartCity.Domain.Models;

namespace WeiXinPF.Modules.CDRT.SmartCity.Infrastructure.Repositories
{
    public class MessageRepository : IMessageRepository
    {
        private readonly SmartContext _context;
        public MessageRepository(SmartContext context)
        {
            _context = context;
        }
        public void Add(Message entity)
        {
            _context.Message.Add(entity);
        }

        public void Modify(Message entity)
        {
            _context.Message.Attach(entity);
            _context.Entry(entity).State = EntityState.Modified;
        }

        public void Delete(int entityId)
        {
            var entity = Get(entityId);
            _context.Message.Remove(entity);
        }

        public void Delete(Expression<Func<Message, bool>> predicate)
        {
            var systementitys = GetAllList(predicate);
            _context.Message.RemoveRange(systementitys);
        }

        #region 根据条件查询并排序  +  T GetModel<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderLambda, bool isAsc = true)
        public IQueryable<Message> GetAllList<TKey>(Expression<Func<Message, bool>> where, Expression<Func<Message, TKey>> order, bool isAsc)
        {
            if (isAsc)
            {
                return _context.Message.Where(where).OrderBy(order);
            }
            else
            {
                return _context.Message.Where(where).OrderByDescending(order);
            }
        }
        #endregion

        public IQueryable<Message> GetAllList(Expression<Func<Message, bool>> predicate)
        {
            return _context.Message.Where(predicate).OrderByDescending(p=>p.create_time);
        }

        public IQueryable<Message> GetAllList()
        {
            return _context.Message;
        }

        public Message Get(int id)
        {
            return _context.Message.SingleOrDefault(b => b.message_id == id);
        }

        public void Delete(Message entity)
        {
            _context.Message.Remove(entity);
        }

        public void DeleteList(List<Message> list)
        {
            _context.Message.RemoveRange(list);
        }

        public void SaveChange()
        {
            try
            {
                _context.SaveChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}",
                                                validationError.PropertyName,
                                                validationError.ErrorMessage);
                    }
                }
            }

        }

        public void Modify(IQueryable<Message> Messages)
        {
            Messages.ToList().ForEach(c =>
            {
                _context.Message.Attach(c);
                _context.Entry(c).State = EntityState.Modified;
            });
        }

       
    }
}